﻿//
// Created by hujin on 2020/10/6.
//

#include "symbolTable.h"

bool SymbolTable::pushSymbol(const std::string& name, const Identifier_Data& type) {
	if (table.size() == stackLevel) table.emplace_back();
	if (table[stackLevel].count(name))
        return false;
	table[stackLevel][name] = type;
	table[stackLevel][name].uid = ++uidpos;
    table[stackLevel][name].global = stackLevel == 0;
    return true;
}

void SymbolTable::pushStack() {
	table.emplace_back();
	stackLevel++;
}

void SymbolTable::popStack() {
	if (table.size() <= stackLevel)return;
	table[stackLevel].clear();
	stackLevel--;
}


Identifier_Data SymbolTable::getDiscriptions(const std::basic_string<char>& name) {
	for (int i = std::min(stackLevel, (int)table.size() - 1) ; i >= 0; --i) {
		if (table[i].count(name))return table[i][name];
	}
	return {};
}

bool SymbolTable::containsSymbol(std::string& name) {
	for (int i = std::min(stackLevel, (int)table.size() - 1); i >= 0; --i) {
		if (table[i].count(name))return true;
	}
	return false;
}

